package cn.edu.uestc.topo;

import java.util.UUID;

import org.json.JSONObject;

public class Link {

    private UUID id = null;
    private String name = null;
    private UUID srcNodeId = null;
    private UUID srcPortId = null;
    private UUID dstNodeId = null;
    private UUID dstPortId = null;
    private int type = 1;
    private int status = 2;
    private int metric = 0;
    private int attributeFlags = 0;
    private int flashDegree = 0;
    private long bandwidth = 0;
    private long reservableBandwidth = 0;
    private long allocatedBandwidth = 0;
    private int delay = 0;
    private int packetLossRate = 0;
    private int policy = 0;

    public Link() {

    }

    public UUID getId() {
        return id;
    }

    public void setId(UUID id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public UUID getSrcNodeId() {
        return srcNodeId;
    }

    public void setSrcNodeId(UUID srcNodeId) {
        this.srcNodeId = srcNodeId;
    }

    public UUID getSrcPortId() {
        return srcPortId;
    }

    public void setSrcPortId(UUID srcPortId) {
        this.srcPortId = srcPortId;
    }

    public UUID getDstNodeId() {
        return dstNodeId;
    }

    public void setDstNodeId(UUID dstNodeId) {
        this.dstNodeId = dstNodeId;
    }

    public UUID getDstPortId() {
        return dstPortId;
    }

    public void setDstPortId(UUID dstPortId) {
        this.dstPortId = dstPortId;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public int getMetric() {
        return metric;
    }

    public void setMetric(int metric) {
        this.metric = metric;
    }

    public int getAttributeFlags() {
        return attributeFlags;
    }

    public void setAttributeFlags(int attributeFlags) {
        this.attributeFlags = attributeFlags;
    }

    public long getBandwidth() {
        return bandwidth;
    }

    public void setBandwidth(long bandwidth) {
        this.bandwidth = bandwidth;
    }

    public long getReservableBandwidth() {
        return reservableBandwidth;
    }

    public void setReservableBandwidth(long reservableBandwidth) {
        this.reservableBandwidth = reservableBandwidth;
    }

    public long getAllocatedBandwidth() {
        return allocatedBandwidth;
    }

    public void setAllocatedBandwidth(long allocatedBandwidth) {
        this.allocatedBandwidth = allocatedBandwidth;
    }

    public int getDelay() {
        return delay;
    }

    public void setDelay(int delay) {
        this.delay = delay;
    }

    public int getPacketLossRate() {
        return packetLossRate;
    }

    public void setPacketLossRate(int packetLossRate) {
        this.packetLossRate = packetLossRate;
    }

    public int getPolicy() {
        return policy;
    }

    public void setPolicy(int policy) {
        this.policy = policy;
    }

    public void readFromJson(JSONObject jsonObject) {
        try {
            this.id = UUID.fromString(jsonObject.getString("id"));
            this.name = jsonObject.getString("name");
            this.srcNodeId = UUID.fromString(jsonObject.getString("srcNodeId"));
            this.srcPortId = UUID.fromString(jsonObject.getString("srcPortId"));
            this.dstNodeId = UUID.fromString(jsonObject.getString("dstNodeId"));
            this.dstPortId = UUID.fromString(jsonObject.getString("dstPortId"));
            this.status = jsonObject.getInt("status");
            this.type = jsonObject.getInt("type");
            this.metric = jsonObject.getInt("metric");
            this.flashDegree = jsonObject.getInt("flashDegree");
            this.attributeFlags = jsonObject.getInt("attributeFlags");
            this.bandwidth = jsonObject.getLong("bandwidth");
            this.reservableBandwidth = jsonObject.getLong("reservableBandwidth");
            this.allocatedBandwidth = jsonObject.getLong("allocatedBandwidth");
            this.delay = jsonObject.getInt("delay");
            this.packetLossRate = jsonObject.getInt("packetLossRate");
            this.packetLossRate = jsonObject.getInt("policy");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public JSONObject writeToJson() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("id", this.id.toString());
            jsonObject.put("name", this.name);
            jsonObject.put("srcNodeId", this.srcNodeId.toString());
            jsonObject.put("srcPortId", this.srcPortId.toString());
            jsonObject.put("dstNodeId", this.dstNodeId.toString());
            jsonObject.put("dstPortId", this.dstPortId.toString());
            jsonObject.put("status", this.status);
            jsonObject.put("type", this.type);
            jsonObject.put("metric", this.metric);
            jsonObject.put("flashDegree", this.flashDegree);
            jsonObject.put("attributeFlags", this.attributeFlags);
            jsonObject.put("bandwidth", this.bandwidth);
            jsonObject.put("reservableBandwidth", this.reservableBandwidth);
            jsonObject.put("allocatedBandwidth", this.allocatedBandwidth);
            jsonObject.put("delay", this.delay);
            jsonObject.put("packetLossRate", this.packetLossRate);
            jsonObject.put("policy", this.policy);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

    @Override
    public String toString() {
String stringContent = null;
        try {
            stringContent = this.writeToJson().toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return stringContent;
    }
}
